package com.example.order.service;
import com.alibaba.boot.dubbo.annotation.DubboConsumer;
import com.alibaba.fastjson.JSONObject;
import com.example.common.Dto;
import com.example.common.DtoUtil;
import com.example.pojo.GdOrder;
import com.example.pojo.GdUser;
import com.example.service.RpcLoginService;
import com.example.service.RpcOrderService;
import com.example.utils.Constants;
import com.example.utils.EmptyUtils;
import com.example.vo.OrderMessageVo;
import com.example.vo.OrderVo;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.jms.Message;
import javax.jms.ObjectMessage;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * Created by LENOVO on 2018/9/23.
 */
@Component
public class OrderServiceImpl implements OrderService {

    @DubboConsumer
    private RpcOrderService rpcOrderService;

    @DubboConsumer
    private RpcLoginService rpcLoginService;

    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Override
    public Dto<List<OrderVo>> queryOrderList(HttpServletRequest request) throws Exception {
        List<GdOrder> gdOrderList=null;
        List<OrderVo> gdOrderVoList=new ArrayList<OrderVo>();
        //获取用户
        String token = request.getHeader("token");
        String info=stringRedisTemplate.opsForValue().get(token);
        GdUser user = JSONObject.parseObject(info,GdUser.class);
        if(EmptyUtils.isEmpty(user)){
            return DtoUtil.returnFail("用户未登录","0001");
        }
        Map<String,Object> param=new HashMap<String, Object>();
        param.put("userId",user.getGd());
        try {
            gdOrderList=rpcOrderService.getOrderListByMap(param);
            for (GdOrder gdOrder:gdOrderList){
                OrderVo orderVo=new OrderVo();
                BeanUtils.copyProperties(gdOrder,orderVo);
                gdOrderVoList.add(orderVo);
            }
            return DtoUtil.returnDataSuccess(gdOrderVoList);
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("0002","系统异常");
        }
    }

    @Override
    public boolean cancelOrderStatus() throws Exception {
        //刷新商品的库存
        boolean flag = false;
        try{
            //刷新订单状态
            flag = rpcOrderService.flushOrderStatus();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            return flag;
        }
    }

    /***
     * 接收生成订单的消息
     * @param message
     */
    @JmsListener(destination = Constants.QueueName.TO_CREATE_ORDER)
    public void receiveOrderQueue(Message message)throws Exception {

        ObjectMessage objMsg = (ObjectMessage) message;
        try {

            OrderMessageVo orderMessageVo=(OrderMessageVo) objMsg.getObject();
            rpcOrderService.createOrder(orderMessageVo);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /***
     * 接收修改订单状态的消息
     * @param message
     */
    @JmsListener(destination = Constants.QueueName.TO_UPDATED_ORDER_QUEUE)
    public void receiveUpdateOrderQueue(Message message)throws Exception {

        ObjectMessage objMsg = (ObjectMessage) message;
        try {
            OrderMessageVo orderMessageVo=(OrderMessageVo) objMsg.getObject();
            rpcOrderService.updateOrder(orderMessageVo.getOrderNo(),orderMessageVo.getTradeNo());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
